import { defineStore } from 'pinia';

import { quasarExtras, quasarExtrasSvgIcons } from 'src/IconSet';

const modules = import.meta.glob('/**/*.mjs');

let inter: NodeJS.Timeout;

export type iconType = Array<{
  iconTypeName: string;
  fromStr: string;
  iconNames: Array<string>;
}>;

export type iconListType = Array<{
  label: string;
  icon: string;
}>;

const iconsAllList: {
  // 自带的图标集
  quasarExtras: iconType;
  quasarExtrasSvgIcons: iconType;
} = {
  quasarExtras: quasarExtras.map((i) => {
    return {
      iconNames: i.iconNames,
      fromStr: i.fromStr,
      iconTypeName: i.iconTypeName,
    };
  }),
  quasarExtrasSvgIcons: quasarExtrasSvgIcons.map((i) => {
    return {
      iconNames: i.iconNames,
      fromStr: i.fromStr,
      iconTypeName: i.iconTypeName,
    };
  }),
};

export const useIconStore = defineStore('iconstore', {
  state: (): {
    queryText: string;
    iconClickItem: { info: iconType[0]; iconList: iconListType } | undefined;
  } => ({
    queryText: '',
    iconClickItem: undefined,
  }),
  getters: {},
  actions: {
    queryIconList() {
      if (this.queryText != '') {
        const query = this.queryText;
        const tempQuasarExtras: iconType = [];
        const tempQuasarExtrasSvgIcons: iconType = [];

        for (let index = 0; index < iconsAllList.quasarExtras.length; index++) {
          const element = iconsAllList.quasarExtras[index];

          for (const key in element.iconNames) {
            const iconStr = element.iconNames[key];

            if (
              iconStr.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()) !=
              -1
            ) {
              tempQuasarExtras.push(element);
              break;
            }
          }
        }

        for (
          let index = 0;
          index < iconsAllList.quasarExtrasSvgIcons.length;
          index++
        ) {
          const element = iconsAllList.quasarExtrasSvgIcons[index];

          for (const key in element.iconNames) {
            if (Object.prototype.hasOwnProperty.call(element.iconNames, key)) {
              const iconStr = element.iconNames[key];
              if (
                iconStr
                  .toLocaleLowerCase()
                  .indexOf(query.toLocaleLowerCase()) != -1
              ) {
                tempQuasarExtrasSvgIcons.push(element);
                break;
              }
            }
          }
        }

        return {
          quasarExtras: tempQuasarExtras,
          quasarExtrasSvgIcons: tempQuasarExtrasSvgIcons,
        };
      } else {
      }
      return {
        quasarExtras: iconsAllList.quasarExtras,
        quasarExtrasSvgIcons: iconsAllList.quasarExtrasSvgIcons,
      };
    },
    loadIcon(item: iconType[0]) {
      if (inter) {
        clearInterval(inter);
      }
      // 首先把你需要动态路由的组件地址全部获取
      //let modules = import.meta.glob('../**/*.vue')
      // 然后动态路由的时候这样来取
      //return modules['../' + path + '.vue']
      const path = item.fromStr.replaceAll('/', '');

      modules[`/icon_modules/${path}.mjs`]().then(
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (res: any) => {
          const query = this.queryText;
          const temp: iconListType = [];
          for (const key in res) {
            const element = res[key];
            if (query != '') {
              if (
                key.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()) != -1
              ) {
                temp.push({
                  icon: element,
                  label: key,
                });
              }
            } else {
              temp.push({
                icon: element,
                label: key,
              });
            }
          }
          this.iconClickItem = {
            info: item,
            iconList: [],
          };

          const tempArr: Array<iconListType> = [];
          const size = 50;
          while (temp.length) {
            tempArr.push(temp.splice(0, size));
          }
          this.iconClickItem?.iconList.push(...tempArr[0]);
          const tL = tempArr.length;
          let ii = 1;
          inter = setInterval(() => {
            this.iconClickItem?.iconList.push(...tempArr[ii]);
            if (ii == tL - 1) {
              if (inter) {
                clearInterval(inter);
              }
            }
            ii++;
          }, 1000);
        },
      );
    },
  },
});
